<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Escort_user_roles_n_zones_entity{
	var $current_dbprefix = "";
	var $username = '';
	var $group = 0;
	var $country_id = 0;
	var $state_id = 0;
	var $province_id = 0;
	var $city_id = 0;
	var $updated_time = 0;
	var $_ci = null;
		
	function __construct() {
		$this->_ci= & get_instance();
		$this->current_dbprefix = $this->_ci->db->dbprefix;
		$this->_ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$this->_ci= & get_instance();
		$this->_ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setUserName($input){
		$this->username=$input;
	}
	function setGroup($input){
		$this->group = $input;
	}
	function setCountryId($input){
		$this->country_id = $input;
	}
	function setState_id($input){
		$this->state_id = $input;
	}
	function setProvinceId($input){
		$this->province_id = $input;
	}
	function setCityId($input){
		$this->city_id = $input;
	}
	function setUpdatedTime($input){
		$this->updated_time= $input;
	}
	
	function getUserName(){
		return $this->username;
	}
	function getGroup(){
		return $this->group;
	}
	function getCountryId(){
		return $this->country_id;
	}
	function getStateId(){
		return $this->state_id;
	}
	function getProviceId(){
		return $this->province_id ;
	}
	function getCityId(){
		return $this->city_id;
	}
	function getUpdatedTime(){
		return $this->updated_time;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		if($cmd == 'username')
		{
			$result = $ci->db->where('username',$input)->get(TBL_ESCORT_USER_ROLES_N_ZONES)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->username = $result[0]->username;
			$this->group = $result[0]->group;
			$this->country_id = $result[0]->country_id;
			$this->state_id = $result[0]->state_id;
			$this->province_id = $result[0]->province_id;
			$this->city_id = $result[0]->city_id;
			$this->updated_time = $result[0]->updated_time;
			return $this;
		}
		return false;
	}
	
	function getUserRolesNZones($username){
		$ci=& get_instance();
		$this->_ci->db->set_dbprefix('');
		$this->_ci->db->where('username',$username);
		return $this->_ci->db->get(TBL_ESCORT_USER_ROLES_N_ZONES)->result();
	}
	function getUserRolesNZonesByGroupID($groupId){
		$ci=& get_instance();
		$ci->db->where('group',$groupId);
		return $ci->db->get(TBL_ESCORT_USER_ROLES_N_ZONES)->result();
	}
		
	function save(){
		$ci=& get_instance();
		$ci->db->set('username',$this->getUserName());
		$ci->db->set('group',$this->getGroup());
		$ci->db->set('country_id',$this->getCountryId());
		$ci->db->set('province_id',$this->getProviceId());
		$ci->db->set('city_id',$this->getCityId());
		$ci->db->set('updated_time',$this->getUpdatedTime());
		$ci->db->insert(TBL_ESCORT_USER_ROLES_N_ZONES);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set('username',$this->getUserName());
		$ci->db->set('group',$this->getGroup());
		$ci->db->set('country_id',$this->getCountryId());
		$ci->db->set('province_id',$this->getProviceId());
		$ci->db->set('city_id',$this->getCityId());
		$ci->db->set('updated_time',$this->getUpdatedTime());
		return $ci->db->where('username', $this->username)->update(TBL_ESCORT_USER_ROLES_N_ZONES);
	}
	function delete($username){
		$ci=& get_instance();
		$ci->db->where('username',$username);
		$ci->db->delete(TBL_ESCORT_USER_ROLES_N_ZONES);
	}
	function getStateList($username, $group, $countryId){
		$ci=& get_instance();
		if($group == USER_ADMIN){
			return getStateData_ioc($countryId);
		}
		
		$zoneList = $this->getUserRolesNZones($username);
		$stateList = array();
		if($zoneList){
			$stateIdList = array();
			foreach ($zoneList as $zone){
				if($zone->country_id == $countryId){
					if($zone->state_id == 0){
						return getStateData_ioc($countryId);
					}else{
						$stateIdList[] = $zone->state_id;
					}
				}
			}
			
			foreach ($stateIdList as $stateId){
				$stateData = $ci->states_entity->init('id', $stateId);
				if($stateData){
					$stateList[$stateData->getId()] = $stateData->getStateName();
				}
			}
		}
		return $stateList;
	}
	function getProvinceList($username, $group, $countryId, $stateId){
		$ci=& get_instance();
		$country = $ci->countries_entity->init('id',$countryId);
		if(!$country || $country->getCountryCode() != "ES"){
			return array();
		}
		if($group == USER_ADMIN){
			return getProvinceData_ioc($countryId, $stateId);
		}
		
		$zoneList = $this->getUserRolesNZones($username);
		$provinceList = array();
		if($zoneList){
			$provinceIdList = array();
			foreach ($zoneList as $zone){
				if($zone->country_id == $countryId && $zone->state_id == $stateId){
					if($zone->province_id == 0){
						return getProvinceData_ioc($countryId, $stateId);
					}else{
						$provinceIdList[] = $zone->province_id;
					}
				}
			}
			
			foreach ($provinceIdList as $provinceId){
				$provinceData = $ci->provinces_spain_entity->init('id', $provinceId);
				if($provinceData){
					$provinceList[$provinceData->getId()] = $provinceData->getProvinceNameSpanish();
				}
			}
		}
		return $provinceList;
	}
	function getCityList($username, $group, $countryId, $stateId, $provinceId){
		$ci=& get_instance();
		$country = $ci->countries_entity->init('id',$countryId);
		if(!$country){
			return array();
		}
		if($country->getCountryCode() == "ES"){
			if($group == USER_ADMIN){
				return getCityData_ioc($countryId, $stateId, $provinceId);
			}
			$zoneList = $this->getUserRolesNZones($username);
			$cityList = array();
			if($zoneList){
				$cityIdList = array();
				foreach ($zoneList as $zone){
					if($zone->country_id == $countryId && $zone->state_id == $stateId&& $zone->province_id == $provinceId){
						if($zone->city_id == 0){
							return getCityData_ioc($countryId, $stateId, $provinceId);
						}else{
							$cityIdList[] = $zone->city_id;
						}
					}
				}
				
				foreach ($cityIdList as $cityId){
					$cityData = $ci->cities_entity->init('id', $cityId);
					if($cityData){
						$cityList[$cityData->getId()] = $cityData->getCityName();
					}
				}
			}
		}else{
			if($group == USER_ADMIN){
				return getCityData_ioc($countryId, $stateId, $provinceId);
			}
			$zoneList = $this->getUserRolesNZones($username);
			$cityList = array();
			if($zoneList){
				$cityIdList = array();
				foreach ($zoneList as $zone){
					if($zone->country_id == $countryId && $zone->state_id == $stateId && $zone->province_id == $provinceId){
						if($zone->city_id == 0){
							return getCityData_ioc($countryId, $stateId, $provinceId);
						}else{
							$cityIdList[] = $zone->city_id;
						}
					}
				}
				
				foreach ($cityIdList as $cityId){
					$cityData = $ci->cities_entity->init('id', $cityId);
					if($cityData){
						$cityList[$cityData->getId()] = $cityData->getCityName();
					}
				}
			}
		}
		return $cityList;
	}
	public function checkValidZones($subZoneList, $zoneList){
		$ci = &get_instance();
		$isSubZone = true;
		foreach ($subZoneList as $subZone){
			if(!$isSubZone){
				return false;
			}
			foreach ($zoneList as $zone){
				if ($zone->country_id == 0){
					return true;
				}
				if($zone->country_id == $subZone->country_id){
					if($zone->state_id == 0){
						$isSubZone = true;
						break;
					}else{
						if($zone->state_id == $subZone->state_id){
							if($zone->province_id == 0){
								$COUNTRY = $ci->countries_entity->init('id',$zone->country_id);
								if($COUNTRY->getCountryCode() == 'ES'){
									$isSubZone = true;
									break;
								}else{
									if($zone->city_id == 0 || ($zone->city_id == $subZone->city_id)){
										$isSubZone = true;
										break;
									}else{
										$isSubZone = false;
									}
								}
							}else{
								if($zone->province_id == $subZone->province_id){
									if($zone->city_id == 0 || ($zone->city_id == $subZone->city_id)){
										$isSubZone = true;
										break;
									}else{
										$isSubZone = false;
									}
								}else{
									$isSubZone = false;
								}
								
							}					
						}else{
							$isSubZone = false;
						}
					}
				}else{
					$isSubZone = false;
				}
			}
		}
		return $isSubZone;
	}
}